在開始實作之前,讓我們稍微複習 HPA 是什麼:
HorizontalPodAutoscaler(簡稱 HPA ) 自動更新工作負載資源(例如 Deployment 或者 StatefulSet),目的是自動擴縮工作負載以滿足需求。HPA 的重點是調整 Pod 的數量以適應實際負載。
叢集必需部署 Metrics Server 。因為 HPA 需要透過 kubelet 收集 Metrics Server。部署安裝的方式可以回頭去看之前的章節。
組態檔案: hpa-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
resources:
limits:
cpu: 250m
requests:
cpu: 100m
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
labels:
run: nginx-svc
spec:
ports:
- port: 80
selector:
app: nginx
kubectl apply -f hpa-example.yaml
kubectl get deployment,pod,service
輸出如下
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1/1 1 1 119s
NAME READY STATUS RESTARTS AGE
pod/nginx-769cfd889-wkgm5 1/1 Running 0 102s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
[...]
service/nginx-svc ClusterIP 10.96.250.1 <none> 80/TCP 119s
kubectl autoscale deployment nginx --cpu-percent=50 --min=1 --max=10
或使用以下組態檔案
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
namespace: default
spec:
maxReplicas: 10
metrics:
- resource:
name: cpu
target:
averageUtilization: 50
type: Utilization
type: Resource
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
HPA 控製器將增加和減少副本的數量 (通過更新 Deployment)以保持所有 Pod 的平均 CPU 利用率為 50%。 Deployment 然後更新 ReplicaSet —— 這是所有 Deployment 在 Kubernetes 中工作方式的一部分 —— 然後 ReplicaSet 根據其 .spec
的更改新增或刪除 Pod。
kubectl get hpa
---
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx cpu: 0%/50% 1 10 1 27s
CPU 利用率是 0%,是因為我們尚未傳送任何請求到 nginx 伺服器。
接下來,看看自動擴縮器如何對增加的負載做出反應。
t1
,啟動一個 Pod 作為客戶端,並向 nginx service 不斷傳送查詢請求kubectl run -it load-generator --rm --image=busybox:1.36 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O - nginx; done"
完成後,監控以下資源:
t2
,監控 HPA 的變化kubectl get hpa nginx --watch
t3
,監控 Deployment nginx
事件kubectl get events --watch --field-selector involvedObject.kind=Deployment,involvedObject.name=nginx
等待幾分鐘,查看結果:
t2
,可以看到 CPU 負載升高,然後又因為副本增加而又下降NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx cpu: 0%/50% 1 10 1 29m
nginx Deployment/nginx cpu: 10%/50% 1 10 1 29m
nginx Deployment/nginx cpu: 90%/50% 1 10 1 29m
nginx Deployment/nginx cpu: 90%/50% 1 10 2 30m
nginx Deployment/nginx cpu: 50%/50% 1 10 2 30m
nginx Deployment/nginx cpu: 52%/50% 1 10 2 31m
nginx Deployment/nginx cpu: 55%/50% 1 10 2 32m
nginx Deployment/nginx cpu: 57%/50% 1 10 3 32m
nginx Deployment/nginx cpu: 45%/50% 1 10 3 32m
nginx Deployment/nginx cpu: 37%/50% 1 10 3 32m
nginx Deployment/nginx cpu: 38%/50% 1 10 3 33m
nginx Deployment/nginx cpu: 36%/50% 1 10 3 33m
t3
,可以看到 Deployment 觸發了 Scale up 事件LAST SEEN TYPE REASON OBJECT MESSAGE
0s Normal ScalingReplicaSet deployment/nginx Scaled up replica set nginx-7fcc74d4cf to 2 from 1
0s Normal ScalingReplicaSet deployment/nginx (combined from similar events): Scaled up replica set nginx-7fcc74d4cf to 3 from 2
查詢 Deployment
kubectl get deployments nginx
---
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 42m
可以看到,Deployment 與 HPA 對應的副本數量相同。HPA 已經為增加的負載做出資源的調整。
現在,讓我們關閉剛剛的 Pod 客戶端,看看 HPA 會如何反應。
t1
,輸入 <Ctrl> + C
來終止負載的產生。等待幾分鐘,查看結果:
t2
,可以看到 CPU 負載升高,然後又因為副本增加而又下降NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx cpu: 36%/50% 1 10 3 3m45s
nginx Deployment/nginx cpu: 28%/50% 1 10 3 4m16s
nginx Deployment/nginx cpu: 1%/50% 1 10 3 4m31s
nginx Deployment/nginx cpu: 0%/50% 1 10 3 5m1s
nginx Deployment/nginx cpu: 0%/50% 1 10 3 9m1s
nginx Deployment/nginx cpu: 0%/50% 1 10 2 9m16s
nginx Deployment/nginx cpu: 0%/50% 1 10 1 9m31s
nginx Deployment/nginx cpu: 0%/50% 1 10 1 9m46s
t3
,可以看到 Deployment 觸發了 Scale up 事件LAST SEEN TYPE REASON OBJECT MESSAGE
0s Normal ScalingReplicaSet deployment/nginx Scaled down replica set nginx-7fcc74d4cf to 2 from 3
0s Normal ScalingReplicaSet deployment/nginx Scaled down replica set nginx-7fcc74d4cf to 1 from 2
60m Normal ScalingReplicaSet deployment/nginx Scaled up replica set nginx-769cfd889 to 1
kubectl get deployments nginx
---
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 23m
可以看到,Deployment 與 HPA 對應的副本數量相同。